﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.display.*;
    import org.aswing.geom.*;

    public class AssetPane extends Container {

        public static const PREFER_SIZE_BOTH:int = 0;
        public static const PREFER_SIZE_IMAGE:int = 1;
        public static const PREFER_SIZE_LAYOUT:int = 2;
        public static const SCALE_NONE:int = 0;
        public static const SCALE_FIT_PANE:int = 1;
        public static const SCALE_STRETCH_PANE:int = 2;
        public static const SCALE_FIT_WIDTH:int = 3;
        public static const SCALE_FIT_HEIGHT:int = 4;
        public static const SCALE_CUSTOM:int = 5;
        public static const CENTER:int = 0;
        public static const TOP:int = 1;
        public static const LEFT:int = 2;
        public static const BOTTOM:int = 3;
        public static const RIGHT:int = 4;

        protected var asset:DisplayObject;
        protected var assetContainer:DisplayObjectContainer;
        protected var assetMask:Shape;
        private var assetVisible:Boolean;
        private var maskFloor:Boolean;
        private var floorLoaded:Boolean;
        private var prefferSizeStrategy:int;
        private var verticalAlignment:int;
        private var horizontalAlignment:int;
        private var scaleMode:int;
        private var customScale:int;
        private var actualScale:int;
        private var floorOriginalSize:IntDimension;
        private var floorOriginalScaleX:Number;
        private var floorOriginalScaleY:Number;
        private var hadscaled:Boolean;
        private var offsetX:Number;
        private var offsetY:Number;

        public function AssetPane(_arg1:DisplayObject=null, _arg2:int=1){
            this.prefferSizeStrategy = _arg2;
            this.verticalAlignment = TOP;
            this.horizontalAlignment = LEFT;
            this.scaleMode = SCALE_NONE;
            this.actualScale = 100;
            this.customScale = 100;
            this.hadscaled = false;
            this.maskFloor = true;
            this.floorOriginalSize = null;
            this.assetVisible = true;
            this.floorLoaded = false;
            this.offsetX = 0;
            this.offsetY = 0;
            this.floorOriginalScaleX = 1;
            this.floorOriginalScaleY = 1;
            setFocusable(false);
            this.assetContainer = AsWingUtils.createSprite(this, "assetContainer");
            this.assetMask = AsWingUtils.createShape(this, "assetMask");
            this.assetMask.graphics.beginFill(0xFF0000);
            this.assetMask.graphics.drawRect(0, 0, 1, 1);
            this.assetMask.visible = false;
            this.setAsset(_arg1);
        }
        public function setAsset(_arg1:DisplayObject):void{
            if (this.asset != _arg1){
                if (this.asset){
                    if (this.asset.parent == this.assetContainer){
                        this.assetContainer.removeChild(this.asset);
                    };
                };
                this.asset = _arg1;
                if (_arg1){
                    this.storeOriginalScale();
                    this.assetContainer.addChild(_arg1);
                };
                this.setLoaded(!((_arg1 == null)));
                this.resetAsset();
            };
        }
        public function unloadAsset():void{
            this.setAsset(null);
        }
        protected function storeOriginalScale():void{
            if (this.asset){
                this.floorOriginalScaleX = this.asset.scaleX;
                this.floorOriginalScaleY = this.asset.scaleY;
            };
        }
        protected function resetAsset():void{
            if (this.asset){
                this.asset.scaleX = this.floorOriginalScaleX;
                this.asset.scaleY = this.floorOriginalScaleY;
                this.setAssetOriginalSize(new IntDimension(this.asset.width, this.asset.height));
                this.asset.visible = this.assetVisible;
            };
            revalidate();
        }
        public function getAsset():DisplayObject{
            return (this.asset);
        }
        public function setPrefferSizeStrategy(_arg1:Number):void{
            this.prefferSizeStrategy = _arg1;
        }
        public function getPrefferSizeStrategy():Number{
            return (this.prefferSizeStrategy);
        }
        public function getVerticalAlignment():Number{
            return (this.verticalAlignment);
        }
        public function setVerticalAlignment(_arg1:int=0):void{
            if (_arg1 == this.verticalAlignment){
                return;
            };
            this.verticalAlignment = _arg1;
            revalidate();
        }
        public function getHorizontalAlignment():Number{
            return (this.horizontalAlignment);
        }
        public function setHorizontalAlignment(_arg1:int=4):void{
            if (_arg1 == this.horizontalAlignment){
                return;
            };
            this.horizontalAlignment = _arg1;
            revalidate();
        }
        public function setScaleMode(_arg1:int):void{
            if (this.scaleMode != _arg1){
                this.scaleMode = _arg1;
                revalidate();
            };
        }
        public function getScaleMode():int{
            return (this.scaleMode);
        }
        public function setCustomScale(_arg1:int):void{
            this.setScaleMode(SCALE_CUSTOM);
            if (this.customScale != _arg1){
                this.customScale = _arg1;
                revalidate();
            };
        }
        public function getActualScale():int{
            return (this.actualScale);
        }
        public function getCustomScale():int{
            return (this.customScale);
        }
        public function setOffsetX(_arg1:Number):void{
            if (this.offsetX != _arg1){
                this.offsetX = _arg1;
                revalidate();
            };
        }
        public function setOffsetY(_arg1:Number):void{
            if (this.offsetY != _arg1){
                this.offsetY = _arg1;
                revalidate();
            };
        }
        public function getOffsetX():Number{
            return (this.offsetX);
        }
        public function getOffsetY():Number{
            return (this.offsetY);
        }
        public function setAssetVisible(_arg1:Boolean):void{
            this.assetVisible = _arg1;
            if (this.asset){
                this.asset.visible = _arg1;
            };
        }
        public function isAssetVisible():Boolean{
            return (this.assetVisible);
        }
        public function isLoaded():Boolean{
            return (this.floorLoaded);
        }
        public function getAssetOriginalSize():IntDimension{
            if (this.isLoaded()){
                return (this.floorOriginalSize);
            };
            return (null);
        }
        override public function doLayout():void{
            super.doLayout();
            this.fitImage();
        }
        private function fitImage():void{
            var _local1:DisplayObject;
            var _local2:IntRectangle;
            var _local3:IntDimension;
            if (this.isLoaded()){
                _local1 = this.getAsset();
                _local2 = getPaintBounds();
                _local3 = this.countFloorSize();
                this.assetMask.x = _local2.x;
                this.assetMask.y = _local2.y;
                this.assetMask.width = _local2.width;
                this.assetMask.height = _local2.height;
                if (this.scaleMode == SCALE_STRETCH_PANE){
                    _local1.x = (_local2.x - this.offsetX);
                    _local1.y = (_local2.y - this.offsetY);
                    _local1.width = _local3.width;
                    _local1.height = _local3.height;
                    this.hadscaled = true;
                } else {
                    if ((((((((this.scaleMode == SCALE_FIT_PANE)) || ((this.scaleMode == SCALE_FIT_WIDTH)))) || ((this.scaleMode == SCALE_FIT_HEIGHT)))) || ((this.scaleMode == SCALE_CUSTOM)))){
                        _local1.width = _local3.width;
                        _local1.height = _local3.height;
                        this.alignFloor();
                        this.hadscaled = true;
                    } else {
                        if (this.hadscaled){
                            if (_local1.width != this.floorOriginalSize.width){
                                _local1.width = this.floorOriginalSize.width;
                            };
                            if (_local1.height != this.floorOriginalSize.height){
                                _local1.height = this.floorOriginalSize.height;
                            };
                            this.hadscaled = false;
                        };
                        this.alignFloor();
                    };
                };
                if (this.scaleMode != SCALE_STRETCH_PANE){
                    this.actualScale = Math.floor(((_local1.width / this.floorOriginalSize.width) * 100));
                } else {
                    this.actualScale = 0;
                };
                this.setMaskAsset(this.maskFloor);
            };
        }
        private function alignFloor(_arg1:IntRectangle=null):void{
            var _local3:Number;
            var _local4:Number;
            var _local2:DisplayObject = this.getAsset();
            if (_arg1 == null){
                _arg1 = getPaintBounds();
            };
            if (this.horizontalAlignment == CENTER){
                _local3 = (_arg1.x + ((_arg1.width - _local2.width) / 2));
            } else {
                if (this.horizontalAlignment == RIGHT){
                    _local3 = (_arg1.x + (_arg1.width - _local2.width));
                } else {
                    _local3 = _arg1.x;
                };
            };
            if (this.verticalAlignment == CENTER){
                _local4 = (_arg1.y + ((_arg1.height - _local2.height) / 2));
            } else {
                if (this.verticalAlignment == BOTTOM){
                    _local4 = (_arg1.y + (_arg1.height - _local2.height));
                } else {
                    _local4 = _arg1.y;
                };
            };
            _local2.x = (_local3 - this.offsetX);
            _local2.y = (_local4 - this.offsetY);
        }
        override protected function countPreferredSize():IntDimension{
            var _local2:IntDimension;
            var _local1:IntDimension = super.countPreferredSize();
            if (this.isLoaded()){
                _local2 = this.countFloorSize();
                _local2 = getInsets().getOutsideSize(_local2);
            } else {
                _local2 = _local1;
            };
            if (this.prefferSizeStrategy == PREFER_SIZE_IMAGE){
                return (_local2);
            };
            if (this.prefferSizeStrategy == PREFER_SIZE_LAYOUT){
                return (_local1);
            };
            return (new IntDimension(Math.max(_local2.width, _local1.width), Math.max(_local2.height, _local1.height)));
        }
        private function countFloorSize():IntDimension{
            var _local3:Number;
            var _local4:Number;
            var _local5:Number;
            var _local1:IntRectangle = getPaintBounds();
            var _local2:IntDimension = new IntDimension();
            if (this.scaleMode == SCALE_STRETCH_PANE){
                _local2.width = _local1.width;
                _local2.height = _local1.height;
            } else {
                if ((((((this.scaleMode == SCALE_FIT_PANE)) || ((this.scaleMode == SCALE_FIT_WIDTH)))) || ((this.scaleMode == SCALE_FIT_HEIGHT)))){
                    _local3 = (this.floorOriginalSize.width / _local1.width);
                    _local4 = (this.floorOriginalSize.height / _local1.height);
                    _local5 = 1;
                    if (this.scaleMode == SCALE_FIT_WIDTH){
                        _local5 = _local3;
                    } else {
                        if (this.scaleMode == SCALE_FIT_HEIGHT){
                            _local5 = _local4;
                        } else {
                            _local5 = Math.max(_local3, _local4);
                        };
                    };
                    _local2.width = (this.floorOriginalSize.width / _local5);
                    _local2.height = (this.floorOriginalSize.height / _local5);
                } else {
                    if (this.scaleMode == SCALE_CUSTOM){
                        _local2.width = (this.floorOriginalSize.width * (this.customScale / 100));
                        _local2.height = (this.floorOriginalSize.height * (this.customScale / 100));
                    } else {
                        _local2.width = (this.floorOriginalSize.width - this.offsetX);
                        _local2.height = (this.floorOriginalSize.height - this.offsetY);
                    };
                };
            };
            return (_local2);
        }
        public function reload():void{
            this.resetAsset();
        }
        public function isMaskAsset():Boolean{
            return (this.maskFloor);
        }
        public function setMaskAsset(_arg1:Boolean):void{
            this.maskFloor = _arg1;
            this.applyMaskAsset();
        }
        protected function applyMaskAsset():void{
            if (this.maskFloor){
                this.assetMask.visible = true;
                this.assetContainer.mask = this.assetMask;
            } else {
                this.assetContainer.mask = null;
                this.assetMask.visible = false;
            };
        }
        protected function setLoaded(_arg1:Boolean):void{
            this.floorLoaded = _arg1;
        }
        protected function setAssetOriginalSize(_arg1:IntDimension):void{
            this.floorOriginalSize = new IntDimension(_arg1.width, _arg1.height);
        }
        protected function loadFloor():void{
        }

    }
}//package org.aswing 
